var ws,uid,c_id,member_name,spage='';
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('id');
	member_truename = plus.storage.getItem('name');
	member_username = plus.storage.getItem('user');
	member_name = member_truename?member_truename:member_username;
	c_id = ws.c_id;
	spage = ws.spage;  
	
	
	$('.mobilepic').on('click',function(){
		plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"拍照添加"},{title:"相册添加"}]},function(e){
			if(e.index == 1){
				var car =plus.camera.getCamera();
				car.captureImage(function(path){
					//展示图片
//					alert(plus.io.convertLocalFileSystemURL(path));
//					$('.shangchuan').attr('src','file://' + plus.io.convertLocalFileSystemURL(path));
					appendpic(path);
					
				},function(err){});
				
			}else if(e.index == 2){
				plus.gallery.pick(function(path){
//					$('.shangchuan').attr('src',path);
					appendpic(path);
				});
			}
		});
	})
	
	
	
	
	
	
	
//	console.log(circle+ "/index.php?act=theme&op=save_reply&c_id="+c_id+"&t_id="+t_id+"&u_id="+uid+"&type=mobile"+answer_id);
	$(".send").on('click',function(){
		var name = $(".name").val();
		var themecontent = $(".themecontent").val();
		var images = '';
		$('.bbs-pulish-img img').each(function(){
			images += $(this).attr('src') + ',';
		})  
		if (!name){
			toast("请输入标题");return;
		}
		if(name.length < 4 || name.length > 30){
			toast("标题在4到30字符之间");return;
		}
		if(!themecontent){
			toast("内容不能为空");return;
		}
		console.log(circle+ "/index.php?act=theme&op=save_theme&c_id="+c_id+"&type=mobile&inajax=1");
		$.ajax({ 
			type:"post",
			url:circle+ "/index.php?act=theme&op=save_theme&c_id="+c_id+"&type=mobile&inajax=1",
			data:{ 
				form_submit:'ok',
				themecontent:themecontent,
				name:name,
				member_name:member_name,
				u_id:uid,
				images:images
			},
			dataType:'json',
			success:function(data){
				console.log(JSON.stringify(data));
				if(data == 1){
					//这里要返回页面并刷新详情页面
					toast("发布成功！");  
					plus.webview.getWebviewById(spage).reload();
					setTimeout(function(){
						ws.close();    
					},500)
				}else{
					toast("发布失败！");
				}
			},
			error:function(e){
				console.log(JSON.stringify(e));
			}
			
		})
	})
})

//添加图片
var index = 1;
var files = [];
function appendpic(p){
	plus.nativeUI.showWaiting('图片处理中...');
//		console.log(p); 
	//创建新的路径
//				var newpath = p.replace(/\./g , new Date().gettime() + '.');
	var newpath = p.replace(/\./g , new Date().getTime()+'.');
	
	plus.zip.compressImage({
			src : p,
			dst : newpath,
			quality : 20
		},
		function(){//毁掉成功
			files.push({name : "uploadkey" + index,path:p});
			index++;
			upload();
		},
		function(error){
		alert('压缩图片失败');
		plus.nativeUI.closeWaiting();
	})
}

function upload(){
	if(files.length <= 0){
		toast("请上传头像！");
	}
//				console.log(JSON.stringify(files.path));return; 
	var server = apiRoot + "?act=sns_album&op=file_uploadone&member_id="+uid;  
	var task = plus.uploader.createUpload(server,
	    {method:"post"},
		function(t,status){ //上传完成
			if(status == 200){ 
				plus.nativeUI.closeWaiting();
//					console.log(123);
				var infopics = $.parseJSON(t.responseText);
					console.log(JSON.stringify(infopics));
				if(infopics.code == 200){ 
					$('.bbs-pulish-img').prepend('<img src="'+ infopics.datas.file_url +'" data-preview-src="" data-preview-group="1"/>');
				}else{
					toast(infopics.datas.error);
				}
//					plus.nativeUI.toast('处理完成' + t.responseText);
			}else{
				toast('上传失败：' + status);
				plus.nativeUI.closeWaiting();
			}
			
		},function () {
			plus.nativeUI.closeWaiting();
		}
		
	); 
	for(var i = 0;i < files.length ; i++){
		var f =files[i];
		task.addFile(f.path,{key:'file'});
//					    console.log(f.name)
	}
	task.start();
}		



